package com.ruoyi.common.tools.excel.listener;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;

import com.alibaba.excel.context.AnalysisContext;
import com.ruoyi.common.core.service.DictService;
import com.ruoyi.common.tools.constant.ERecordConstant;
import com.ruoyi.common.tools.excel.base.MyExcelListener;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.erecord.region.service.IRegionService;
import com.ruoyi.standingbook.evaluate.domain.vo.EvaluateImportDetailVo;

public class EvaluateExcelListener extends MyExcelListener<EvaluateImportDetailVo> {

	private DictService dictService = SpringUtils.getBean(DictService.class);
	private IRegionService regionService = SpringUtils.getBean(IRegionService.class);
	
	
	private String batchNo = null;

	public EvaluateExcelListener(String batchNo) {
		this.batchNo = batchNo;
	}

	@Override
	public void doValidate(EvaluateImportDetailVo data, AnalysisContext context) {
		// 1.设置批次号
		// 2.校验数据 与 匹配数据字典数据 设置对应字段
		// 3.设置 处理状态 与 处理信息
		List<String> validateList = new ArrayList<>();

		data.setBatchNo(this.batchNo);

		if (!StringUtils.isEmpty(data.getServiceTypeName())) {
			int val = NumberUtils
					.toInt(this.dictService.getDictValue("erecord_samplebox_type", data.getServiceTypeName()), -1);
			if (val == -1) {
				validateList.add("[ 类型 ] 匹配不到数据");
			} else {
				data.setServiceTypeId(val);
			}
		} else {
			validateList.add("[ 类型 ] 为空");
		}

    	if (!StringUtils.isEmpty(data.getRegionName())) {
    		String val = this.regionService.getRegionIdByName(data.getRegionName());
    		if (StringUtils.isEmpty(val)) {
    			//validateList.add("[ 所属区域 ] 匹配不到数据");
    		}else {
    			data.setRegionId(val);
    		}
    	}
    	
    	if (!StringUtils.isEmpty(data.getStreetName())) {
    		String val = this.regionService.getRegionIdByName(data.getStreetName());
    		if (StringUtils.isEmpty(val)) {
    			//validateList.add("[ 所属街道 ] 匹配不到数据");
    		}else {
    			data.setStreetId(val);
    		}
    	}

		if (!validateList.isEmpty()) {
			String errMsg = String.join(" ，", validateList);
			excelResult.getValidateList().add(errMsg);

			data.setProcessMsg(errMsg);
			data.setProcessStatus(ERecordConstant.COMMON_STATUS_ERROR);
		} else {
			data.setProcessMsg("成功");
			data.setProcessStatus(ERecordConstant.COMMON_STATUS_SUCCESS);
		}
	}

	@Override
	protected void doOnExceptionProcess(String errorMsg) {
		EvaluateImportDetailVo vo = new EvaluateImportDetailVo();
		vo.setBatchNo(this.batchNo);
		vo.setProcessStatus(ERecordConstant.COMMON_STATUS_ERROR);
		vo.setProcessMsg(errorMsg);
		this.excelResult.getList().add(vo);
	}

}
